package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.print.DocFlavor;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/*
    套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    /*
        新增套餐功能
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除该分类下所有缓存
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /*
    根据id查询套餐属性
    */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto dishDto = setmealService.getByIdWithDish(id);
        return R.success(dishDto);
    }

    /*
        套餐分页查询
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Setmeal::getName,name);
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list =  records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);

            Long categoryId = item.getCategoryId();
            //根据分类id获取对象
            Category categoryServiceById = categoryService.getById(categoryId);
            if(categoryServiceById != null){
                //分类名称
                String categoryName = categoryServiceById.getName();
                setmealDto.setCategoryName(categoryName);//设置分类名称
            }
            return setmealDto;
        }).collect(Collectors.toList());

        Page<SetmealDto> dtoPage = setmealDtoPage.setRecords(list);

        return R.success(dtoPage);

    }

    /*
        修改套餐信息
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除该分类下所有缓存
    public  R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /*
    启用/停售菜品
 */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)//删除该分类下所有缓存
    public R<String> status(@PathVariable Integer status,@RequestParam List<Long> ids){
        //定义setmeals用于重新封装后存入数据库
        List<Setmeal> setmeals = new ArrayList<>();

        //循环遍历id，修改菜品状态并将数据存入dishes列表
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmeals.add(setmeal);
        }
        //将列表数据存入数据库
        setmealService.updateBatchById(setmeals);
        return R.success("售卖状态修改成功");
    }

    /*
        删除套餐
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除该分类下所有缓存
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /*
    前端-查询套餐数据
 */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();

        queryWrapper.eq(categoryId != null,Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(status != null,Setmeal::getStatus,status);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }

}
